(function(){
    angular.module('app').controller("agbAccountCtrl",function($scope,proxy,$location) {
        $scope.permission = PERMISSION;
        $scope.isEditInfo = false;
        $scope.isEditPwd = false;
        $scope.isEditOem = false;
        $scope.isEditAdLogo = false;
        $scope.oem = JSON.parse(localStorage.oem)
        $scope.hasOEM = false;

        // $scope.editInfo = function(){
        //     $scope.isEditInfo = !$scope.isEditInfo;
        // }
        // $scope.editPWD = function(){
        //     $scope.isEditPwd = !$scope.isEditPwd;
        // }
        // $scope.editOEM = function(){
        //     $scope.isEditOem = !$scope.isEditOem;
        // }
        $scope.switchEditStatus = function(type){
            $scope[type] = !$scope[type];
        }
        $scope.layer = layer;
         //提示信息显示
        $scope.tips = function (event,str) {
            layer.tips(str, $(event.target), {
                time: 0,
                tips: [1, '#fffee3']
            });
        }
        // 代理商密码对象
        $scope.pwd = {}

        // 修改代理商密码
        $scope.resetPWD = function() {
            var pwd = $scope.pwd;
            if(!pwd.oldPasswd) return layer.msg("请输入旧密码!");
            if(pwd.newPasswd != pwd.newPasswd2) return layer.msg("两次密码不一致!");
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/reset/passwd',
                load:true,
                data: {
                    uid: localStorage.userId,
                    oldPasswd: pwd.oldPasswd,
                    newPasswd: pwd.newPasswd,
                },
                success: function(){
                    layer.closeAll();
                    layer.msg("修改成功!");
                    $scope.isEditPwd = !$scope.isEditPwd;
                }
            })
        }

        // 修改代理商
        $scope.modifyAgent = function() {
            var agent = $scope.agent,
                domainReg = /^https?:\/\//,
                phoneReg = /^1\d{10}$/;
            if(!domainReg.test(agent.wwwUrl)) return layer.msg("域名格式有误!");
            if(!phoneReg.test(agent.telephone)) return layer.msg("手机号码格式有误!");

            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/advertiser/modify',
                load:true,
                data: {
                    data:{
                        id: agent.id,
                        name: agent.name,
                        address: agent.address,
                        telephone: agent.telephone,
                        wwwUrl: agent.wwwUrl,
                        contacts: agent.contacts,
                    }
                },
                success: function(res){
                    layer.msg("修改成功!");
                    getBaseInfo();
                    $scope.isEditInfo = false;
                }
            })
        }
        
        $scope.uploadImg = function (obj) {
            $scope.imgObj = obj;
            $("#upload").trigger("click");
        }

        $scope.saveOEM = function () {
            var oem = $scope.oem;

            oem.url = (oem.url||"").replace(/http(s)?:\/\//,"").split("/")[0];
            if(!oem.url) return layer.msg("请输入自有域名")


            oem.hostname = oem.url;
            oem.sdkLogo = oem.staticLogo;

            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/user/oem/update',
                load: true,
                data: oem,
                success: function(){
                    layer.msg("修改成功");
                    $scope.isEditOem = false;
                    // OEM信息
                    proxy.proxy({
                        'url': STATIC_URL.domian+'/dsp/user/oem/query',
                        'method':'post',
                        'data': {
                            hostname: $location.host()
                        },
                        success: function (res) {
                            var data = res;
                            $scope.oem = {
                                url: data.url || "",//自有域名
                                background: data.background || "http://res1.hubcloud.com.cn/dsp/clientInfo/2018/4/10/1523329438394.png",// 登陆页面背景图片
                                loginLogo: data.loginLogo || "./images/login_logo.png",//登陆页面Logo地址
                                staticLogo: data.staticLogo || "./images/logo.png",//导航栏Logo地址
                                sdkLogo: data.sdkLogo || "",//SDK Logo地址
                                iconUrl: data.iconUrl || "",//角标图片地址
                                title: data.title || "投放端",//所有oem文字信息
                                copyright: data.copyright || "",//版权信息
                                adLogo: data.adLogo || "",
                                adLogoExt: data.adLogoExt || "",
                                adLogoUrl: data.adLogoUrl || "",
                                adLabel: data.adLabel || ""
                            }

                            localStorage.oem = JSON.stringify($scope.oem);
                        }
                    });
                }
            })
        }

        function getBaseInfo() {
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/advertiser',
                load: true,
                data: {
                    id:localStorage.userId
                },
                success: function(res){
                    // 获得列表
                    $scope.agent = res.data;
                }
            })
        }
        function hasOEM() {
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/sys/parameter/query',
                load: true,
                data: {
                    "filter":{"feature":"dsp.feature.agency.oem.edit.allow,"}
                },
                success: function(res){
                    if(res.data && res.data[0].value == "0") {
                        $scope.hasOEM = false;
                    } else {
                        $scope.hasOEM = true;

                        // 上传文件按钮
                        fileUpload({
                            'btnId': "upload",
                            'funcUrl': '1',
                            'FileUploaded':function(info){
                                $scope.$apply(function(){
                                    $scope.oem[$scope.imgObj.attr] = info.url;
                                })
                            },
                            'BeforeUpload': function (up, file, next) {
                                var reg = /.png$/i,
                                    nFile = file.getNative(),
                                    image = new Image(),
                                    size = $scope.imgObj.size,
                                    width = $scope.imgObj.width,
                                    height = $scope.imgObj.height;
                    
                                // 判断文件类型
                                if(!reg.test(file.name)){
                                    up.removeFile(file);
                                    return alert("格式错误，请上传png格式。")
                                }

                                // 判断文件大小
                                if(nFile.size >= size*1024) {
                                    up.removeFile(file);
                                    return alert("文件不得大于"+size+"K")
                                }

                                if(width && height) {
                                    //加载图片获取图片真实宽度和高度
                                    image.onload= function() {
                                        // 判断图片比例
                                        if(image.width != width || image.height != height){
                                            up.removeFile(file);
                                            alert("图片的尺寸不是"+width+"x"+height+"，请重新上传")
                                        } else {
                                            next();
                                        }
                                    };
                                } else {
                                    next();
                                }
                    
                                image.src = window.URL.createObjectURL(nFile);
                            }
                        })
                    }
                }
            })
        }


        hasOEM();
        getBaseInfo();
    })
})();